package com.neusoft.a312cxy.android18cxy.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.neusoft.a312cxy.android18cxy.R;
import com.neusoft.a312cxy.android18cxy.activity.ShaiTuInfoActivity;
import com.neusoft.a312cxy.android18cxy.bean.ShaiTuListBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Young on 2017/9/25.
 */

public class ShaiTuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context mContext;//承载上下文
    private List<ShaiTuListBean> mDataList;//动态数组承载数据使用
    private LayoutInflater mLayoutInflater;
    private String img1;

    public ShaiTuAdapter(Context mContext, List<ShaiTuListBean> mDataList){
        this.mContext=mContext;
        this.mDataList=mDataList;
        mLayoutInflater=LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=mLayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_shaitu,parent,false);
        return new ViewHolder(v);
    }

    //将数据绑定到控件
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final  ShaiTuListBean entity=mDataList.get(position);
        if (null==entity)
            return;
        ViewHolder viewHolder= (ViewHolder) holder;
        viewHolder.club_name.setText(entity.getClub_name());
        viewHolder.introduction.setText(entity.getIntroduction());
        viewHolder.user_name.setText(entity.getUser_name());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent=new Intent(mContext, ShaiTuInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("shaitu_id",entity.getId());
                bundle.putString("userid",entity.getUser_id());
                bundle.putString("photo",entity.getThumb());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
        if(!entity.getThumb().isEmpty())
        {
            Picasso.with(mContext).load("http://172.24.10.175/workout/Uploads/"+entity.getThumb()).into(viewHolder.img);
        }
        else
        {
            viewHolder.img.setImageResource(R.drawable.ic_club);
        }
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    //找到控件，将其初始化
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        TextView club_name;
        TextView user_name;
        TextView introduction;
        public ViewHolder(View itemView) {
            super(itemView);
            img= (ImageView) itemView.findViewById(R.id.thumb);
            user_name= (TextView) itemView.findViewById(R.id.user_name);
            club_name= (TextView) itemView.findViewById(R.id.club_name);
            introduction=(TextView)itemView.findViewById(R.id.introduction);
        }
    }
}
